Code
library(dplyr)
library(sqldf)
Tony Duan
October 4, 2022
Rows: 5
Columns: 8
$ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or…
$ height <int> 172, 167, 96, 202, 150
$ mass <dbl> 77, 75, 32, 136, 49
$ hair_color <chr> "blond", NA, NA, "none", "brown"
$ skin_color <chr> "fair", "gold", "white, blue", "white", "light"
$ eye_color <chr> "blue", "yellow", "red", "yellow", "brown"
$ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0
$ sex <chr> "male", "none", "none", "male", "female"
选
# A tibble: 5 × 1
sex
<chr>
1 male
2 none
3 none
4 male
5 female
添
name height mass hair_color skin_color eye_color birth_year sex
1 Luke Skywalker 172 77 blond fair blue 19.0 male
2 C-3PO 167 75 <NA> gold yellow 112.0 none
3 R2-D2 96 32 <NA> white, blue red 33.0 none
4 Darth Vader 202 136 none white yellow 41.9 male
5 Leia Organa 150 49 brown light brown 19.0 female
male_flag
1 1
2 0
3 0
4 1
5 0
# A tibble: 5 × 9
name height mass hair_color skin_color eye_color birth_year sex male_flag
<chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <dbl>
1 Luke … 172 77 blond fair blue 19 male 1
2 C-3PO 167 75 <NA> gold yellow 112 none 0
3 R2-D2 96 32 <NA> white, bl… red 33 none 0
4 Darth… 202 136 none white yellow 41.9 male 1
5 Leia … 150 49 brown light brown 19 fema… 0
{janitor} by Sam Firke
document by Albert Rapp: https://albert-rapp.de/posts/07_janitor_showcase/07_janitor_showcase.html https://www.youtube.com/watch?v=AKPvlNWZBEQ
---
title: "{dplyr}包数据整理"
author: "Tony Duan"
date: "2022-10-04"
categories: [packages]
execute:
warning: false
error: false
format:
html:
code-fold: show
code-tools: true
---
# 变量选添删
```{r}
library(dplyr)
library(sqldf)
```
```{r}
#| echo: false
starwars_sample=starwars %>% select(c(1:8)) %>% head(5)
```
```{r}
glimpse(starwars_sample)
```
# 变量选添删
选
```{r}
sqldf("select sex from starwars_sample")
```
```{r}
starwars_sample %>% select(sex)
```
添
```{r}
sqldf("select *,case when sex='male' then 1 else 0 end as male_flag from starwars_sample")
```
```{r}
starwars_sample %>% mutate(male_flag=ifelse(sex=='male',1,0))
```
# 观察选添删
# 转置,汇总
# Reference
{janitor} by Sam Firke
document by Albert Rapp: https://albert-rapp.de/posts/07_janitor_showcase/07_janitor_showcase.html https://www.youtube.com/watch?v=AKPvlNWZBEQ